/**
 * author:Administrator
 * date:2016/3/15
 * desc: manager command 接口
 */

'use strict';

let api = {};
module.exports = api;

let sagaAll = require('../domain/saga');
let locRepo = require('../domain/model_cmd/location/locationRepo.js');
let Location = require('../domain/model_cmd/location/Location.js');
let cateRepo = require('../domain/model_cmd/category/categoryRepo.js');
let Category = require('../domain/model_cmd/category/Category.js');

/**
 * 创建 category
 * @param {String}    name
 * @param {Number}    pid
 */
api.createCategory = function *(name, pid) {
  let pojo = {name: name};
  if (pid) {
    let parent = yield cateRepo.find(pid);
    if (!parent) return false;
    pojo.pid = parent.id;
  }

  let saga = new sagaAll.CategoryCreate(pojo);
  let result = yield saga.result();
  // console.log('createCategory Result', result);
  return result;
};

/**
 * destroy category
 * @param {Number}      id
 * @param {Boolean}     isForce
 */
api.destroyCategory = function *(id, isForce) {
  let category = yield cateRepo.find(id);
  if (!category) return;

  let saga = new sagaAll.CategoryDestroy({
    category: Category.rebuild(category),
    isForce: isForce
  });
  let result = yield saga.result();
  // console.log('destroyCategory Result', result);
  return result;
};

/**
 * 创建 Location
 * @param {String}    name
 * @param {Number}    pid
 */
api.createLocation = function *(name, pid) {
  let pojo = {name: name};
  if (pid) {
    let parent = yield locRepo.find(pid);
    if (!parent) return false;
    pojo.pid = parent.id;
  }

  let saga = new sagaAll.LocationCreate(pojo);
  let result = yield saga.result();
  // console.log(result);
  return result;
};

/**
 * destroy Location
 * @param {Number}      id
 * @param {Boolean}     isForce
 */
api.destroyLocation = function *(id, isForce) {
  let location = yield locRepo.find(id);
  if (!location) return;

  let saga = new sagaAll.LocationDestroy({
    location: Location.rebuild(location),
    isForce: isForce
  });
  let result = yield saga.result();
  // console.log('destroyCategory Result', result);
  return result;
};